package com.gewei.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gewei.constant.MessageConstant;
import com.gewei.entity.PageResult;
import com.gewei.entity.QueryPageBean;
import com.gewei.entity.Result;
import com.gewei.pojo.User;
import com.gewei.service.UserService;
import com.gewei.utils.DateUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 用户服务
     */
    @Reference
    private UserService userService;

    /**
     * 获取当前登录用户的用户名
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception {
        //  当 SpringSecurity 完成认证后，会将当前用户信息保存到框架提供的上下文对象（底层基于 Session）
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    /**
     * 新增
     *
     * @param user
     * @param roles
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roles) {

        try {
            userService.add(user, roles);
        } catch (Exception e) {
            //  新增失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //  新增成功
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        User user = null;
        try {
            user = userService.findById(id);
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(user);
            return result;
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findRoleByUser")
    public Result findRoleByUser(Integer id) {
        try {
            List<Integer> roles = userService.findRoleByUser(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roles) {
        try {
            userService.edit(user, roles);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.deleteById(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<User> userList = userService.findAll();
        if (userList != null && userList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(userList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}